import NProgress from "nprogress";
import router from "./router";
import 'nprogress/nprogress.css';
import {getToken} from "./uilts/token";
import store from "./store";

NProgress.configure({
    easing: 'ease',  // 动画方式
    speed: 300,  // 递增进度条的速度
    showSpinner: false, // 是否显示加载ico
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3 // 初始化时的最小百分比
})
//全局前置守卫
router.beforeEach(async (to, from, next) => {
    NProgress.start();
    if (getToken()) {
        if (store.state.userinfo.name) {
            next()
        } else {
            await store.dispatch('getUserInfo')
            await store.dispatch('getMenu')
            next()
        }
    } else {
        if (to.path === '/') {
            next()
        } else {
            next('/')
        }
    }
})
router.afterEach((to, from, next) => {
    //关闭进度条
    NProgress.done()
})